package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.StaffDao;
import com.aaa.entity.Staff;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class StaffDaoImpl implements StaffDao {

    private BaseDao baseDao = BaseDao.getInstance();


    @Override
    public Staff login(String staffId, String password) {
        String sql = "select * from staff where staffId = ? and password = ?  and status = 1";

        Object[] params = {staffId,password};
        List<Staff> staffList = baseDao.query(sql,params,Staff.class);

        if (staffList!=null && !staffList.isEmpty()){
            return staffList.get(0);
        }

        return null;
    }

    @Override
    public int updateStaffByStaffId(Staff staff) {
        return 0;
    }

    @Override
    public int addStaffByStaffId(Staff staff) {
        return 0;
    }

    @Override
    public List<Staff> getAllStaffInfo(Integer pageNumber, Integer pageSize, String searchId, String searchName) {
        String sql = "select s.*,r.roleName roleName from staff s,role r where s.roleId = r.id ";
        if (StringUtils.isNotBlank(searchId)) {
            sql += " and staffId = " + searchId;
        }
        if (StringUtils.isNotBlank(searchName)) {
            searchName = "%" + searchName + "%";
            sql += " and staffName like '" + searchName + "'";
        }
        sql += " limit ?,?";
        Object[] params = {pageNumber, pageSize};
        List<Staff> staff = baseDao.query(sql, params, Staff.class);
        return staff;
    }

    @Override
    public int getAllStaffInfoCount(String searchId, String searchName) {
        String sql = "select count(1) len from staff where 1 = 1";
        if (StringUtils.isNotBlank(searchId)) {
            sql += " and staffId = " + searchId;
        }
        if (StringUtils.isNotBlank(searchName)) {
            searchName = "%" + searchName + "%";
            sql += " and staffName like '" + searchName + "'";
        }
        List<Map<String, Object>> maps = baseDao.query(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }
}
